package at.fhj.itm10.eventgallery;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import at.fhj.itm10.eventgallery.configuration.Configuration;
import at.fhj.itm10.eventgallery.modell.CodeType;

public class MainActivity extends Activity {
	
	private ImageButton logoButton;
	private Button scanButton;
	private Button manButton;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
//        // check if there is a connection to the internet
//        ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//        
//        if (conManager.getNetworkInfo(0).getState() != NetworkInfo.State.CONNECTED) {
//        	conManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "hallo");
//		}

        // opens homepage with webbrowser
        logoButton = (ImageButton) findViewById(R.id.main_logoButton);
        logoButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		// open url with browser
        		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + Configuration.getINSTANCE().getServerUrl()));
        		startActivity(browserIntent);
        	}
        });
        
        // opens qr-code reader
        scanButton = (Button) findViewById(R.id.main_scanButton);
        scanButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent("com.google.zxing.client.android.SCAN");
				intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
				startActivityForResult(intent, CodeType.QR_SCAN_REQUEST);
			}
		});
        
        // opens manual input
        manButton = (Button) findViewById(R.id.main_manButton);
        manButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent manIntent = new Intent(MainActivity.this, ManualActivity.class);
				startActivityForResult(manIntent, CodeType.MANUAL_REQUEST);
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		Log.d("own", "result: " + resultCode + " " + requestCode);
		
		if (resultCode == RESULT_OK && (requestCode == CodeType.MANUAL_REQUEST || requestCode == CodeType.QR_SCAN_REQUEST)) {
			Intent loadingIntent = new Intent(MainActivity.this, LoadingActivity.class);
			loadingIntent.putExtra("CODE_TYPE", requestCode);
			if (requestCode == CodeType.QR_SCAN_REQUEST) {
				if (!intent.getStringExtra("SCAN_RESULT_FORMAT").equals("QR_CODE"))
					return;
				
				loadingIntent.putExtra("EVENT_CODE", intent.getStringExtra("SCAN_RESULT"));
				
			} else if (requestCode == CodeType.MANUAL_REQUEST) {
				loadingIntent.putExtra("EVENT_CODE", Configuration.getINSTANCE().getServerUrl() + intent.getStringExtra("EVENT_CODE"));
			}
//			startActivity(loadingIntent);
			startActivityForResult(loadingIntent, CodeType.LOADING_REQUEST);
		} else if (resultCode == RESULT_OK && requestCode == CodeType.LOADING_REQUEST) {
			// start upload view
			Log.d("own", "open uploadView");
			Intent upIntent = new Intent(MainActivity.this, UploadActivity.class);
			startActivity(upIntent);
			
		}
	}
}
